<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品编号" prop="goodsSn">
        <el-input v-model="queryParams.goodsSn" placeholder="请输入商品编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商品名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入商品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="审批状态" prop="approveStatus">
        <el-select v-model="form.approveStatus" multiple placeholder="请选择审批状态">
          <el-option v-for="item in goodsApproveStatus" :key="item.id" :label="item.stausName" :value="item.id"
            @keyup.enter.native="handleQuery">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['good:commodity:goods:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['good:commodity:goods:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['good:commodity:goods:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['good:commodity:goods:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="宣传画廊">
              <div v-for="(imgUrl, index) in props.row.gallery" class="imagecontainer" :key="index">
                <image-preview :src="imgUrl" width="80px" class="img" />
              </div>
            </el-form-item>
            <el-form-item label="商品介绍">
              <span>{{ props.row.brief }}</span>
            </el-form-item>
            <el-form-item label="商品单位">
              <span>{{ props.row.unit }}</span>
            </el-form-item>
            <!--            <el-form-item label="关键字">-->
            <!--              <span>{{ props.row.categoryId }}</span>-->
            <!--            </el-form-item>-->
            <el-form-item label="类目ID">
              <span>{{ props.row.categoryId }}</span>
            </el-form-item>
            <el-form-item label="品牌商ID">
              <span>{{ props.row.brandId }}</span>
            </el-form-item>
            <el-form-item label="审批状态">
              <span>{{ props.row.approveStatus == 0 ? "待审批": props.row.approveStatus == 1 ? "审批通过" : props.row.approveStatus == 2 ? "审批拒绝" : props.row.approveStatus == 4 ? "未提交" : "异常" }}</span>
            </el-form-item>
            <el-form-item label="审批内容">
              <span>{{ props.row.approveMsg }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="商品编号" align="center" prop="goodsSn" />
      <el-table-column label="商品名称" :show-overflow-tooltip="true" align="center" prop="name" />
      <el-table-column label="图片" align="center" prop="picUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.picUrl" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="分享图" align="center" prop="shareUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.shareUrl" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="图片详情" :show-overflow-tooltip="true" align="center" prop="gallery">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleSelTu(scope.row)"
            v-hasPermi="['good:commodity:goods:edit']">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="专柜价格" align="center" prop="counterPrice" />
      <el-table-column label="当前价格" align="center" prop="retailPrice" />
      <el-table-column label="审批状态" align="center" prop="approveStatus">
        <template slot-scope="scope">
          <span>{{scope.row.approveStatus == 0 ? "待审批": scope.row.approveStatus == 1 ? "审批通过" : scope.row.approveStatus == 2 ? "审批拒绝" : scope.row.approveStatus == 4 ? "未提交" : "异常"}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.approveStatus == 0" size="mini" type="text" icon="el-icon-edit"
            @click="handleApproval(scope.row)" v-hasPermi="['good:commodity:goods:edit']">审批</el-button>
          <el-button v-if="scope.row.approveStatus == 1" size="mini" type="text" icon="el-icon-edit"
            @click="handleUpdate(scope.row)" v-hasPermi="['good:commodity:goods:edit']">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['good:commodity:goods:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改商品列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品编号" prop="goodsSn">
          <el-input v-model="form.goodsSn" placeholder="请输入商品编号" />
        </el-form-item>
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="图片" prop="picUrl">
          <el-input v-model="form.picUrl" placeholder="请输入图片" />
        </el-form-item>
        <el-form-item label="分享图" prop="shareUrl">
          <el-input v-model="form.shareUrl" placeholder="请输入分享图" />
        </el-form-item>
        <el-form-item label="图片详情" prop="gallery">
          <el-input v-model="form.gallery" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="专柜价格" prop="counterPrice">
          <el-input v-model="form.counterPrice" placeholder="请输入专柜价格" />
        </el-form-item>
        <el-form-item label="当前价格" prop="retailPrice">
          <el-input v-model="form.retailPrice" placeholder="请输入当前价格" />
        </el-form-item>
        <el-form-item label="商品所属类目ID" prop="categoryId">
          <el-input v-model="form.categoryId" placeholder="请输入商品所属类目ID" />
        </el-form-item>
        <el-form-item label="${comment}" prop="brandId">
          <el-input v-model="form.brandId" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="商品关键字" prop="keywords">
          <el-input v-model="form.keywords" placeholder="请输入商品关键字" />
        </el-form-item>
        <el-form-item label="商品简介" prop="brief">
          <el-input v-model="form.brief" placeholder="请输入商品简介" />
        </el-form-item>
        <el-form-item label="是否上架" prop="isOnSale">
          <el-input v-model="form.isOnSale" placeholder="请输入是否上架" />
        </el-form-item>
        <el-form-item label="${comment}" prop="sortOrder">
          <el-input v-model="form.sortOrder" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="是否新品首发，如果设置则可以在新品首发页面展示" prop="isNew">
          <el-input v-model="form.isNew" placeholder="请输入是否新品首发，如果设置则可以在新品首发页面展示" />
        </el-form-item>
        <el-form-item label="是否人气推荐，如果设置则可以在人气推荐页面展示" prop="isHot">
          <el-input v-model="form.isHot" placeholder="请输入是否人气推荐，如果设置则可以在人气推荐页面展示" />
        </el-form-item>
        <el-form-item label="商品单位，例如件、盒" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入商品单位，例如件、盒" />
        </el-form-item>
        <el-form-item label="商品详细介绍，是富文本格式" prop="detail">
          <el-input v-model="form.detail" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="创建时间" prop="addTime">
          <el-date-picker clearable v-model="form.addTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="浏览量" prop="browse">
          <el-input v-model="form.browse" placeholder="请输入浏览量" />
        </el-form-item>
        <el-form-item label="已销售总量" prop="sales">
          <el-input v-model="form.sales" placeholder="请输入已销售总量" />
        </el-form-item>
        <el-form-item label="逻辑删除" prop="deleted">
          <el-input v-model="form.deleted" placeholder="请输入逻辑删除" />
        </el-form-item>
        <el-form-item label="供货单位" prop="commpany">
          <el-input v-model="form.commpany" placeholder="请输入供货单位" />
        </el-form-item>
        <el-form-item label="批发价格" prop="wholesalePrice">
          <el-input v-model="form.wholesalePrice" placeholder="请输入批发价格" />
        </el-form-item>
        <el-form-item label="审批内容" prop="approveMsg">
          <el-input v-model="form.approveMsg" placeholder="请输入审批内容" />
        </el-form-item>
        <el-form-item label="推广佣金金额" prop="brokeragePrice">
          <el-input v-model="form.brokeragePrice" placeholder="请输入推广佣金金额" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!--  显示商品图片详情  -->
    <el-dialog :title="title" :visible.sync="open1" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item prop="gallery">
          <div v-for="(imgUrl, index) in form.gallery" class="image-preview-item" :key="index">

            <image-preview :src="imgUrl" />
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listGoods,
    getGoods,
    delGoods,
    addGoods,
    updateGoods
  } from "@/api/good/commodity/goods";

  export default {
    name: "Goods",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 商品列表表格数据
        goodsList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        open1: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          goodsSn: null,
          name: null,
          gallery: null,
          approveStatus: null,
        },
        // 表单参数
        form: {},
        goodsApproveStatus: [{
            id: 0,
            stausName: "待审批"
          },
          {
            id: 1,
            stausName: "审批通过"
          },
          {
            id: 2,
            stausName: "审批拒绝"
          },
          {
            id: 4,
            stausName: "未提交"
          },
        ],
        // 表单校验
        rules: {
          goodsSn: [{
            required: true,
            message: "商品编号不能为空",
            trigger: "blur"
          }],
          name: [{
            required: true,
            message: "商品名称不能为空",
            trigger: "blur"
          }],
          approveStatus: [{
            required: true,
            message: "审批状态不能为空",
            trigger: "change"
          }],
          browse: [{
            required: true,
            message: "浏览量不能为空",
            trigger: "blur"
          }],
          sales: [{
            required: true,
            message: "已销售总量不能为空",
            trigger: "blur"
          }],
          brokerageType: [{
            required: true,
            message: "佣金类型不能为空",
            trigger: "change"
          }],
        }
      };
    },
    created() {
      this.getList();

    },
    activated() {
      this.getList(); // 每次组件激活时重新查询
    },
    methods: {
      /** 查询商品列表列表 */
      getList() {
        this.loading = true;
        listGoods(this.queryParams).then(response => {
          this.goodsList = response.rows;
          for (let i = 0; i < this.goodsList.length; i++) {
            this.goodsList[i].gallery = JSON.parse(this.goodsList[i].gallery);
          }
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          goodsSn: null,
          name: null,
          picUrl: null,
          shareUrl: null,
          gallery: null,
          counterPrice: null,
          retailPrice: null,
          approveStatus: null,
          categoryId: null,
          brandId: null,
          keywords: null,
          brief: null,
          isOnSale: null,
          sortOrder: null,
          isNew: null,
          isHot: null,
          unit: null,
          detail: null,
          addTime: null,
          updateTime: null,
          browse: null,
          sales: null,
          deleted: null,
          commpany: null,
          wholesalePrice: null,
          approveMsg: null,
          brokerageType: null,
          brokeragePrice: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.queryParams.approveStatus1 = this.form.approveStatus.join(',');
        console.log(this.queryParams.approveStatus1);
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加商品列表";
      },
      // 审批
      handleApproval(row) {
        console.log(JSON.stringify(row, null, 2));
        this.reset();
        const id = row.id || this.ids
        this.$router.push({
          path: '/good/approval',
          query: {
            id: id,
          }
        })
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        this.$router.push({
          path: '/good/update',
          query: {
            id: id,
          }
        })
      },
      handleSelTu(row) {
        this.reset();
        const id = row.id || this.ids
        getGoods(id).then(response => {
          this.form = response.data;
          this.form.gallery = JSON.parse(this.form.gallery);
          this.open1 = true;
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateGoods(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addGoods(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除商品列表编号为"' + ids + '"的数据项？').then(function() {
          return delGoods(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('good/commodity/goods/export', {
          ...this.queryParams
        }, `goods_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
<style>
  .image-preview-item {
    margin-right: 80px;
  }

  .demo-table-expand {
    font-size: 0;
  }

  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }

  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 80%;
  }

  .imagecontainer {
    display: inline-block;
  }
</style>
